/*
 * @Author       : wang chao
 * @Date         : 2023-03-06 12:37:07
 * @LastEditors: wangchao
 * @LastEditTime: 2024-10-08 15:10:12
 * @FilePath: \JD-RTT-Driver\applications\u_app.c
 * @Description  :
 * Copyright 2023 BingShan, All Rights Reserved.
 */
#include "u_app.h"
#include "u_rtc.h"
#include "u_params.h"
#include "u_data.h"
#include "u_utilits.h"
#include "u_alarm.h"
#include "u_app_start.h"
#include "u_app_stop.h"
#include "u_app_state.h"
#include "u_port.h"
#include "u_ctrl.h"

#define DBG_TAG "APP"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

//
VAR_APP_RUN_MODE current_app_run_mode = APP_DO_STOP;
//
VAR_APP_OPERATION_MODE current_app_op_mode = APP_OP_AUTO;
//
VAR_APP_OPERATION_MODE get_current_app_op_mode(void)
{
    return current_app_op_mode;
}

void app_do_reset_runner(void)
{
    ;
}

void app_core_runner(void)
{
    //
    LOG_I("BMS App Core Runner Startup.a");

    while (1)
    {
        // 检查是否存在一级故障发生
        if (bms_exception_occurred() || is_push_emergency_button())
        {
            current_app_run_mode = APP_DO_STOP;
            set_current_app_stop_mode(STOP_DO_EMERGENCY);
        }
        // 计算电池参数

        //
        if (current_app_op_mode == APP_OP_AUTO)
        {
            //
            switch (current_app_run_mode)
            {
                case APP_DO_START:
                    app_do_start_runner();
                    break;

                case APP_DO_STOP:
                    app_do_stop_runner();
                    break;
            }
        }
        else if (current_app_op_mode == APP_OP_RESET)
        {
            app_do_reset_runner();
        }
        else if (current_app_op_mode == APP_OP_MANUAL)
        {
            ;
        }
        //
        bms_chiller_control();
        //
        bms_heater_control();
        //
        bms_detect_work_state();
        //
        rt_thread_mdelay(CYCLIC_CONTROL_PERIOD_MS);
    }
}
